#include "main.h"
#include "iic.h"

uint8_t send_data = 0;
uint8_t send_mode = 0;
static volatile uint8_t refresh_flag = 0;

//延时
void IIC_delay(void)
{
	u8 t=0;
	while(t--);
}

//起始信号
void I2C_Start(void)
{
	OLED_SDA_Set();
	OLED_SCL_Set();
	// IIC_delay();
	OLED_SDA_Clr();
	// IIC_delay();
	OLED_SCL_Clr();
	// IIC_delay();
}

//结束信号
void I2C_Stop(void)
{
	OLED_SDA_Clr();
	OLED_SCL_Set();
	// IIC_delay();
	OLED_SDA_Set();
}

//等待信号响应
void I2C_WaitAck(void) //测数据信号的电平
{
	OLED_SDA_Set();
	// IIC_delay();
	OLED_SCL_Set();
	// IIC_delay();
	OLED_SCL_Clr();
	// IIC_delay();
}

//写入一个字节
void Send_Byte(u8 dat)
{
    u8 i;
    for(i=0;i<8;i++)
    {
        if(dat&0x80)//将dat的8位从最高位依次写入
        {
            OLED_SDA_Set();
        }
        else
        {
            OLED_SDA_Clr();
        }
        // IIC_delay();
        OLED_SCL_Set();
        // IIC_delay();
        OLED_SCL_Clr();//将时钟信号设置为低电平
        dat<<=1;
    }
}

//发送一个字节
//mode:数据/命令标志 0,表示命令;1,表示数据;
// void OLED_WR_Byte(uint8_t data,uint8_t mode)
// {
// 	I2C_Start();
// 	Send_Byte(0x78);
// 	I2C_WaitAck();
// 	if(mode){Send_Byte(0x40);}
//     else{Send_Byte(0x00);}
// 	I2C_WaitAck();
// 	Send_Byte(data);
// 	I2C_WaitAck();
// 	I2C_Stop();
// }

//发送一个字节
//mode:数据/命令标志 0,表示命令;1,表示数据;
void OLED_WR_Byte(uint8_t data,uint8_t mode)
{
    // send_mode = mode;
    // send_data = data;
    // I2C_TargetAddressConfig(I2C_OLED, I2C_OLED_ADDRESS, I2C_MASTER_WRITE);
    // while(I2C_ReadRegister(I2C_OLED, I2C_REGISTER_SR)&0x100000);

    I2C_TargetAddressConfig(I2C_OLED, I2C_OLED_ADDRESS, I2C_MASTER_WRITE);
    while (!I2C_CheckStatus(I2C_OLED, I2C_MASTER_TX_EMPTY));
    if(mode)
    {
        I2C_SendData(I2C_OLED, 0x40);//data
    }
    else
    {
        I2C_SendData(I2C_OLED, 0x00);//command
    }
    while (!I2C_CheckStatus(I2C_OLED, I2C_MASTER_TX_EMPTY));
    I2C_SendData(I2C_OLED, data);
    I2C_GenerateSTOP(I2C_OLED);
    while (I2C_ReadRegister(I2C_OLED, I2C_REGISTER_SR)&0x80000);
}


void iic_init(void)
{
    { /* Enable peripheral clock                                                                              */
        CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
        CKCUClock.Bit.I2C1 = 1;
        CKCUClock.Bit.AFIO = 1;
        CKCUClock.Bit.PA   = 1;
        CKCU_PeripClockConfig(CKCUClock, ENABLE);
    }

    // AFIO_GPxConfig(OLED_SCL_AF_PORT, OLED_SCL_AF_PIN, AFIO_FUN_GPIO);
    // AFIO_GPxConfig(OLED_SDA_AF_PORT, OLED_SDA_AF_PIN, AFIO_FUN_GPIO);
    // GPIO_PullResistorConfig(OLED_PORT, OLED_SCL_PIN, GPIO_PR_UP);
    // GPIO_PullResistorConfig(OLED_PORT, OLED_SDA_PIN, GPIO_PR_UP);
    // GPIO_WriteOutBits(OLED_PORT, OLED_SCL_PIN, SET);
    // GPIO_WriteOutBits(OLED_PORT, OLED_SDA_PIN, SET);
    // GPIO_DirectionConfig(OLED_PORT, OLED_SCL_PIN, GPIO_DIR_OUT);
    // GPIO_DirectionConfig(OLED_PORT, OLED_SDA_PIN, GPIO_DIR_OUT);

    /* Configure AFIO as I2C function                                                                         */
    AFIO_GPxConfig(OLED_SCL_AF_PORT, OLED_SCL_AF_PIN, AFIO_FUN_I2C);
    AFIO_GPxConfig(OLED_SDA_AF_PORT, OLED_SDA_AF_PIN, AFIO_FUN_I2C);

    { /* I2C Master configuration                                                                             */
        I2C_InitTypeDef  I2C_InitStructure;

        I2C_InitStructure.I2C_GeneralCall = DISABLE;
        I2C_InitStructure.I2C_AddressingMode = I2C_ADDRESSING_7BIT;
        I2C_InitStructure.I2C_Acknowledge = DISABLE;
        I2C_InitStructure.I2C_OwnAddress = 0x00;
        I2C_InitStructure.I2C_Speed = ClockSpeed;
        I2C_InitStructure.I2C_SpeedOffset = 0;
        I2C_Init(I2C_OLED, &I2C_InitStructure);
    }
    /* Enable I2C Matser interrupts                                                                           */
    // I2C_IntConfig(I2C_OLED, I2C_INT_STA | I2C_INT_ADRS | I2C_INT_TXDE, ENABLE);
    // NVIC_EnableIRQ(I2C1_IRQn);   
    I2C_Cmd(I2C_OLED, ENABLE);
}

void I2C1_IRQHandler(void)
{
    switch (I2C_ReadRegister(I2C_OLED, I2C_REGISTER_SR))
    {
        /*-----------------------------Master Transmitter ------------------------------------------------------*/
        case I2C_MASTER_SEND_START:
        break;

        case I2C_MASTER_TRANSMITTER_MODE:
            /* Send the first data to I2C1                                                                        */
            I2C_SendData(I2C_OLED, send_mode?0x40:0x00);
        break;

        case I2C_MASTER_TX_EMPTY:
            /* Send the remainder data to I2C1                                                                  */
            I2C_SendData(I2C_OLED, send_data);
            I2C_GenerateSTOP(I2C_OLED);
        break;

        /*-----------------------------Master Receiver ---------------------------------------------------------*/
        // case I2C_MASTER_RECEIVER_MODE:
        //     /* Enable I2C0 ACK for receiving data                                                                 */
        //     I2C_AckCmd(I2C_OLED, ENABLE);
        // break;

        // case I2C_MASTER_RX_NOT_EMPTY:
        //     I2C_ReceiveData(I2C_OLED);
        //     I2C_AckCmd(I2C_OLED, DISABLE);
        //     I2C_GenerateSTOP(I2C_OLED);
        // break;

        default:
        break;
    }
}
